package com.wnzt.baszh.dao.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.wnzt.baszh.dao.IMedicalShootDao;
import com.wnzt.baszh.util.common.ConstantsUtils;
import com.wnzt.baszh.util.common.PageHelper;
import com.wnzt.baszh.util.common.PageHelperService;
/**
 *  数字化病案管理dao层接口实现类
 * @author dqw
 * @date 2017-3-14
 */
@Repository
public class MedicalShootDaoImpl extends PageHelperService implements IMedicalShootDao{
	
	public static final  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public PageHelper getShootData(Map<String, Object> map) {
		String shootDept = (String) map.get("shootDept");
		String shootDate1 = (String) map.get("shootDate1");
		String shootDate2 = (String) map.get("shootDate2");
		String shootName = (String) map.get("shootName");
		String shootFprn = (String) map.get("shootFprn");
		String shootMode = (String) map.get("shootMode");
		String page = (String) map.get("page");
		String rows = (String) map.get("rows");
		int pageSize = 10;
		int currentPage = 1;
		String where = " 1=1 ";
		if(ConstantsUtils.isNotBank(shootDate1)){
			where += " and a.fcydate >= '"+shootDate1+"'";
		}
		if(ConstantsUtils.isNotBank(shootDate2)){
			Date DATE = new Date();
			try {
				DATE = sdf.parse(shootDate2);
			} catch (ParseException e) {
				e.printStackTrace();
			}
    		 Date date = new Date(DATE.getTime() + 1000 * 60 * 60 * 24);
    		 String Date = sdf.format(date);
			where += " and a.fcydate < '"+Date+"'";
		}
		if(ConstantsUtils.isNotBank(shootName)){
			where += " and a.fname like '%"+shootName+"%'";
		}
		if(ConstantsUtils.isNotBank(shootFprn)){
			where += " and a.fprn = '"+shootFprn+"' or a.fbarcode = '"+shootFprn+"' or a.fzyid = "+shootFprn+"";
		}
		if(ConstantsUtils.isNotBank(shootMode)){
			if(shootMode.equals("0")){ //未拍摄
				where += " and a.fzyid not in (select fzyid from SZH_SHOOTRECORD ) ";
			}else if(shootMode.equals("1")){ //审核已通过
				where += " and a.fzyid in (select fzyid from SZH_SHOOTRECORD where fstatus = '1') ";
			}else if(shootMode.equals("2")){ //已拍摄
				where += " and a.fzyid not in (select fzyid from SZH_SHOOTRECORD where fstatus = '1') ";
			}else if(shootMode.equals("3")){ //全部
				where += " ";
			}
		}
		if(ConstantsUtils.isNotBank(shootDept)){
			 String departNumSql = "select fcal from tworkroom where ftykh='" + shootDept + "'";
	            Map<String, Object> departNumMap = this.jdbcTemplate.queryForMap(departNumSql);
	            String departNumStr = departNumMap.get("fcal").toString().replace(",", "','");
	            where += " and a.fcytykh in ('" + departNumStr + "')";
		}
		if (ConstantsUtils.isNotBank(page)) {
            currentPage = Integer.valueOf(page);
        }
        if (ConstantsUtils.isNotBank(rows)) {
            pageSize = Integer.valueOf(rows);
        }
        String field = " a.fprn ,a.ftimes,a.fname,a.fzyid,a.fsex,a.fbirthday,a.fbarcode,a.fcydate,a.frydate,a.frydept,a.fcydept,c.fstatus ";
        String table = " SZH_BASE a LEFT JOIN (select fzyid from SZH_BAFP GROUP BY fzyid)b ON a.fzyid = b.fzyid "
        		+ " LEFT JOIN SZH_SHOOTRECORD c on a.fzyid = c.fzyid  ";
		return this.queryMssqlSplitPageByProc(field, table, where, "a.fzyid", 1, "a.fzyid", currentPage, pageSize);
	}

	@Override
	public PageHelper getAuditData(Map<String, Object> map) {
		StringBuffer where = new StringBuffer(" 1=1 ");
		String auditDept = (String) map.get("auditDept");
		String auditDate1 = (String) map.get("auditDate1");
		String auditDate2 = (String) map.get("auditDate2");
		String auditName = (String) map.get("auditName");
		String auditFprn = (String) map.get("auditFprn");
		String mode = (String) map.get("mode");
		String page = (String) map.get("page");
		String rows = (String) map.get("rows");
		int pageSize = 10;
		int currentPage = 1;
		//出院日期
		if(ConstantsUtils.isNotBank(auditDate1)){
			where.append(" and a.fcydate >= '"+auditDate1+"' ");
		}
		if(ConstantsUtils.isNotBank(auditDate2)){
			Date DATE = new Date();
			try {
				DATE = sdf.parse(auditDate2);
			} catch (ParseException e) {
				e.printStackTrace();
			}
    		 Date date = new Date(DATE.getTime() + 1000 * 60 * 60 * 24);
    		 String Date = sdf.format(date);
			where.append(" and a.fcydate < '"+Date+"' ");
		}
		if(ConstantsUtils.isNotBank(auditName)){ //姓名
			where.append(" and a.fname like '%"+auditName+"%' ");
		}
		if(ConstantsUtils.isNotBank(auditFprn)){ //病案号
			where.append(" and a.fprn = '"+auditFprn+"' or a.fbarcode = '"+auditFprn+"' or a.fzyid = '"+auditFprn+"' ");
		}
		if(ConstantsUtils.isNotBank(mode)){
			if(mode.equals("0")){ // 0 未审核
				where.append(" and b.FSTATUS = '0' ");
			}else if(mode.equals("1")){ // 1 已审核
				where.append(" and b.FSTATUS = '1'  ");
			}else{ //2 审核未通过
				where.append(" and b.FSTATUS = '2'  ");
				
			}
		}
		if(ConstantsUtils.isNotBank(auditDept)){
			 String departNumSql = "select fcal from tworkroom where ftykh='" + auditDept + "'";
	            Map<String, Object> departNumMap = this.jdbcTemplate.queryForMap(departNumSql);
	            String departNumStr = departNumMap.get("fcal").toString().replace(",", "','");
	            where.append(" and a.fcytykh in ('" + departNumStr + "')");
		}
		if (ConstantsUtils.isNotBank(page)) {
            currentPage = Integer.valueOf(page);
        }
        if (ConstantsUtils.isNotBank(rows)) {
            pageSize = Integer.valueOf(rows);
        }
        String field = " a.fprn ,a.ftimes,a.fzyid,a.fname,a.fsex,a.fbirthday,a.fbarcode,a.fcydate,a.frydate,a.frydept,a.fcydept,a.fzyid,b.fstatus";
        String tables = " SZH_BASE a RIGHT  JOIN SZH_SHOOTRECORD b ON a.fzyid = b.fzyid  ";
		return this.queryMssqlSplitPageByProc(field, tables, where.toString(), "a.fzyid", 1, "a.fzyid", currentPage, pageSize);
	}

	@Override
	public boolean savePicture(Map<String, Object> map) {
		//String fprn = (String) map.get("fprn");
		String fzyid = (String) map.get("fzyid");
	//	int ftimes =  (Integer) map.get("ftimes");
		String address = (String) map.get("address");
		int pageNumber = (Integer) map.get("pageNumber");
		String date = (String) map.get("date");
		String name = (String) map.get("name");
		String code = (String) map.get("code");
		//String base64 = (String) map.get("base64");
		String pictureName = (String) map.get("pictureName");
		Float size = Float.valueOf((Integer) map.get("size"));
		if(ConstantsUtils.isNotBank(fzyid)){
			String sql = "insert into szh_bafp(fbarcode,  ftpfl, ftylx, ftpbh, "
					+ "ftpname, ftpym, ftpdx, fissc, fscdz, ffpy, ffpdate,fifencode,fauditstatu,fzyid,ftype)"
					+ " values('','"+code+"','1','"+ pictureName +"','"+pictureName+"'"
					+ ","+pageNumber+","+size+",'1','"+address+"','"+name+"','"+date+"','0','0','"+fzyid+"','0')";
			this.jdbcTemplate.execute(sql);
			return true;
		}else{
			return false;
		}
	}


/*	@Override
	public void update(String fprn, int ftimes) {
		String update = "update szh_bafp set fifencode ='1' where fprn = '"+fprn+"' and ftimes = "+ftimes+"";
		this.jdbcTemplate.update(update);	
	}

	@Override
	public void updateDecode(String fprn, int ftimes, String number) {
		String update = "update szh_bafp set fifencode ='0' where fprn = '"+fprn+"' and ftimes = "+ftimes+" and ftpbh = '"+number+"'";
		this.jdbcTemplate.update(update);
		
	}*/

	@Override
	public Map<String, Object> auditOperation(Map<String, Object> map) {
		String name = (String) map.get("name");
		String savePath = (String) map.get("savePath");
		String originPath = (String) map.get("originPath");
		//String number = (String) map.get("number");
		String categoryNumber = (String) map.get("categoryNumber");
		//String fprn = (String) map.get("fprn");
		//int ftimes = (Integer) map.get("ftimes");
		String barcode = (String) map.get("barcode");
		String fname = (String) map.get("fname");
		String fcode = (String) map.get("fcode");
		String date = (String) map.get("date");
		String fzyid = (String) map.get("fzyid");
	//	String id = (String) map.get("id");
		if(ConstantsUtils.isNotBank(fzyid)){
			//String update = "update szh_bafp set fscdz = '"+ path +"' where fzyid = "+ fzyid +"";
			String update = "update szh_bafp set fscdz = replace(fscdz,'"+originPath+"','"+savePath+"') where fzyid = '"+ fzyid +"'";
			this.jdbcTemplate.update(update);
			String update2 = "update SZH_SHOOTRECORD set fshr = '"+name+"',fshrbh = '"+fcode+"',fshdate = '"+date+"',fstatus = '1' where fzyid = '"+fzyid+"'";
			this.jdbcTemplate.update(update2);
			map.put("msg", true);
			return map;
		}else{
			map.put("msg", false);
			return map;
		}
	}

	@Override
	public Map<String, Object> errorReport(Map<String, Object> map) {
		String fname = (String) map.get("fname");
		//int ftimes = (Integer) map.get("ftimes");
	//	String fprn = (String) map.get("fprn");
		String imgNumber = (String) map.get("imgNumber");
		String category = (String) map.get("category");
		String text = (String) map.get("text");
		String barcode = (String) map.get("barcode");
		String date = (String) map.get("date");
		String name = (String) map.get("name");
		int pageCode = (Integer) map.get("pageCode");
		String reason = (String) map.get("reason");
		String fzyid = (String) map.get("fzyid");
		String sql2 = "select * from szh_errorsb where fzyid = '"+fzyid+"'  and ftpbh = '"+imgNumber+"'";
		List<Map<String, Object>> list  =this.jdbcTemplate.queryForList(sql2);
		if(list.size() <=0){
			if(ConstantsUtils.isNotBank(fzyid)){
				String sql = "insert into szh_errorsb(fbarcode, fflfzbh, ftpbh, fsbuser, fsbdate, foptype, fremark,freason,fstate,fpagecode,fzyid) "
						+ " values('"+barcode+"','"+category+"','"+imgNumber+"','"+fname+"','"+date+"','1','"+text+"','"+reason+"','0',"+pageCode+",'"+fzyid+"')";
				this.jdbcTemplate.execute(sql);
				map.put("msg", true);
				return map;
			}else{
				map.put("msg", false);
				return map;
			}
		}else{
			map.put("msg", "error");
			return map;
		}
		
	}

	@Override
	public List<Map<String, Object>> getReasonRecord(Map<String, Object> map) {
		String fprn = (String) map.get("fprn");
		int ftimes = (Integer) map.get("ftimes");
		String number = (String) map.get("number");
		String fzyid = (String) map.get("fzyid");
		String sql = "SELECT b.fbasecode,b.ftpym ,b.ftpname,b.fscdz ,a.* from SZH_ERRORSB a left JOIN SZH_BAFP b on a.fprn = b.fprn and a.ftimes = b.ftimes "
				+ " where a.fzyid = '"+fzyid+"' and a.FTPBH = '"+number+"' ";
		List<Map<String, Object>> list = this.jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public List<Map<String, Object>> getTree(Map<String, Object> paramMap) {
		String fzyid = (String) paramMap.get("fzyid");
		String ip = (String) paramMap.get("ip");
		String uuid = (String) paramMap.get("uuid").toString();
		String fcode = (String) paramMap.get("fcode");
		String name = (String) paramMap.get("name");
		String prn = (String) paramMap.get("fprn");
		String ftimes = (String) paramMap.get("ftimes");
		String DATE = (String) paramMap.get("DATE");
		String fprn = "";
		String fname = "";
		String fcydate = "";
		String fcydept = "";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		String where="";
		/**
		 * 1.图片拍摄查szh_bafp
		 * 2.pdf文件查 SZH_BAGDMX
		 */
		if(ConstantsUtils.isNotBank(prn) && ConstantsUtils.isNotBank(ftimes)){
			where=" where b.fprn = '" + prn + "' and b.ftimes="+ftimes;
		}else{ 
			where="where b.fzyid = '"+fzyid+"'";
		}
		String sql ="select b.fname,a.FTPFL,a.ftpym,a.fscdz,a.FID,a.FTPBH,a.ftpname,a.fzyid,a.ftype,b.fprn,b.fcydate,b.fcydept,b.ftimes "
				+ "from szh_bafp a left join SZH_BASE b  on a.fzyid = b.fzyid "+where;
		sql+=" union all ";
		sql+="select b.FNAME,a.FBLCODE FTPLX,0 ftpym,a.FFTPENCRYPTPATH fscdz,a.FID,a.FBLMC FTPBH,a.FBLMC ftpname,b.fzyid,1 ftype,b.fprn,c.fcydate,c.fcydept,b.ftimes"+
			" from SZH_BAGDMX a left join SZH_BAGD b  on a.FGDID = b.FID left join SZH_BASE c on c.FZYID=b.FZYID  "+where;
		 list = this.jdbcTemplate.queryForList(sql);
		//新增浏览记录
		if(list.size() >0){
			for (int i = 0; i < list.size(); i++) {
				fprn = (String) list.get(i).get("fprn");
				fname = (String) list.get(i).get("fname");
				fcydate = (String) list.get(i).get("fcydate").toString();
				fcydept = (String) list.get(i).get("fcydept");
				ftimes = list.get(i).get("ftimes")+"";
				break;
			}
			String sql2  = "insert into szh_browserecord ( uuid, fzyid, ip, fdate, fprn, ftimes, fname, fcydate, fusername, fusercode, fcydept)"
					+ " values('"+uuid+"','"+fzyid+"','"+ip+"','"+DATE+"','"+fprn+"',"+ftimes+",'"+fname+"','"+fcydate+"','"+name+"','"+fcode+"','"+fcydept+"') ";
			this.jdbcTemplate.execute(sql2);
		}
		return list;
	}
	
	@Override
	public List<Map<String, Object>> getListOptions() {
		String sql = "select * FROM SZH_BLLXMX";
		List<Map<String, Object>> list = this.jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public boolean appendImg(Map<String, Object> map) {
		String name = (String) map.get("name");
	//	String fprn = (String) map.get("fprn");
		String fzyid = (String) map.get("fzyid");
	//	int ftimes = (Integer) map.get("ftimes");
		int pageNumber = 0;
		pageNumber = (Integer) map.get("pageNumber");
		String category = (String) map.get("category");
		String originPath = (String) map.get("originPath");
		String fbarcode = (String) map.get("fbarcode");
		String savePath = (String) map.get("savePath");
		String imgName = (String) map.get("imgName");
		//String base64 = (String) map.get("base64");
		Float size =  Float.valueOf((Integer) map.get("size"));
		String date = (String) map.get("date");
		boolean isChildren =  (Boolean) map.get("isChildren");
		//如果直接点击图片，则循环更新
		boolean flag = true;
		if(ConstantsUtils.isNotBank(fzyid)){
			if(isChildren){
				//	String update = " update szh_bafp set ftpbh = '"+fprn+"-"+ftimes+"-"+category+"-"+ (num+1) +"' , ftpym = "+(num+1)+" "
				String update = " update szh_bafp set ftpym = ftpym+1 "
						//+ "where fprn ='"+fprn+"' and ftimes = "+ftimes+" and ftpfl = '"+category+"' and ftpym >= "+ (pageNumber+1) +"";
						+ "where fzyid ='"+fzyid+"' and ftpfl = '"+category+"' and ftpym >= "+ (pageNumber+1) +"";
				this.jdbcTemplate.update(update);
				String insert = "insert into szh_bafp(fbarcode, ftpfl, ftylx, ftpbh, "
						+ "ftpname, ftpym, ftpdx, fissc, fscdz, ffpy, ffpdate,fifencode,fauditstatu,fzyid,ftype)"
						+ " values('"+fbarcode+"','"+category+"','1','"+ imgName +"','"+imgName+"'"
						+ ","+ (pageNumber+1) +","+size+",'1','"+savePath+"','"+name+"','"+date+"','0','0','"+fzyid+"','0')";
				this.jdbcTemplate.execute(insert);
				return flag;
			}else{
				//如果直接点击父类菜单，则直接插入一条记录
				String insert = "insert into szh_bafp(fbarcode, ftpfl, ftylx, ftpbh, "
						+ "ftpname, ftpym, ftpdx, fissc, fscdz, ffpy, ffpdate,fifencode,fauditstatu,fzyid,ftype)"
						+ " values('"+fbarcode+"','"+category+"','1','"+imgName+"','"+imgName+"'"
						+ ","+ pageNumber +","+size+",'1','"+savePath+"','"+name+"','"+date+"','0','0','"+fzyid+"','0')";
				this.jdbcTemplate.execute(insert);
				return flag;
			}
		}else{
			return flag = false;
		}
	}

	@Override
	public boolean repeatShoot(Map<String, Object> map) {
	//	String fprn = (String) map.get("fprn");
		String name = (String) map.get("name");
		String fzyid = (String) map.get("fzyid");
     //   int ftimes = (Integer) map.get("ftimes");
	//	String base64Code = (String) map.get("base64Code");
		Float size = Float.valueOf((Integer)map.get("size"));
		String fbarcode = (String) map.get("fbarcode");
		String saveFileName = (String) map.get("saveFileName");
		String fatherCategory = (String) map.get("fatherCategory");
		int pageNumber = (Integer) map.get("pageNumber");
		String id = (String) map.get("id");
		String imgName = (String) map.get("imgName");
		String date = (String) map.get("date");
		if(ConstantsUtils.isNotBank(id)){
			String update = "update szh_bafp set ftpdx = "+size+" ,fscdz = '"+saveFileName+"', ffpy = '"+name+"' ,ffpdate = '"+date+"' "
					+ " where fid = "+id+"";
			this.jdbcTemplate.update(update);
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean removePicture(Map<String, Object> map) {
	//	String fprn = (String) map.get("fprn");
		String fzyid = (String) map.get("fzyid");
	//	int ftimes = (Integer) map.get("ftimes");
		String fatherCategory = (String) map.get("fatherCategory");	
		int pageNumber = (Integer) map.get("pageNumber");
		String id = (String) map.get("id");
		if(ConstantsUtils.isNotBank(id)){
			String delete = "delete  from szh_bafp where fid = "+id+"";
			this.jdbcTemplate.execute(delete);
			String update = "update szh_bafp set  ftpym = ftpym-1 "
					//+ " where fprn = '"+fprn+"' and ftimes = "+ftimes+" and ftpfl = '"+fatherCategory+"' and ftpym >= "+(pageNumber+1)+" ";
					+ " where fzyid = '"+fzyid+"'  and ftpfl = '"+fatherCategory+"' and ftpym >= "+(pageNumber+1)+" ";
			this.jdbcTemplate.update(update);
			return true;
		}else{
			return false;
		}
	}

	@Override
	public PageHelper getQuestionData(Map<String, Object> map) {
		StringBuffer sb = new StringBuffer(" 1=1 ");
		String fprn = (String) map.get("fprn");
		String fzyid = (String) map.get("fzyid");
		Integer ftimes = (Integer) map.get("ftimes");
		String page = (String) map.get("page");
		String rows = (String) map.get("rows");
		int pageSize = 10;
		int currentPage = 1;
		if(ConstantsUtils.isNotBank(fzyid)){
			sb.append(" and fzyid = '"+fzyid+"' ");
		}
		if (ConstantsUtils.isNotBank(page)) {
            currentPage = Integer.valueOf(page);
        }
        if (ConstantsUtils.isNotBank(rows)) {
            pageSize = Integer.valueOf(rows);
        }
		return this.queryMssqlSplitPageByProc(" * ", "SZH_ERRORSB", sb.toString(), "FSBDATE", 1, "FSBDATE", currentPage, pageSize);
	}

	@Override
	public PageHelper PreviewListSearch(Map<String, Object> map) {
		String date1 = (String) map.get("date1");
		String date2 = (String) map.get("date2");
		String fcydept = (String) map.get("fcydept");
		String fzzDoc = (String) map.get("fzzDoc");
		String fprn = (String) map.get("fprn");
		String ftimes = (String) map.get("ftimes");
		String fbarcode = (String) map.get("fbarcode");
		String fname = (String) map.get("fname");
		String fsex = (String) map.get("fsex");
		String category = (String) map.get("category");
		String fjb = (String) map.get("fjb");
		String page = (String) map.get("page");
		String rows = (String) map.get("rows");
		int pageSize = 10;
		int currentPage = 1;
		String table = "";
		String field = "";
		StringBuffer sb = new StringBuffer(" 1=1 ");
		if(ConstantsUtils.isNotBank(date1)){
			sb.append(" and a.fcydate >= '"+date1+"' ");
		}
		if(ConstantsUtils.isNotBank(date2)){
			try {
				Date DATE = new Date();
				DATE = sdf.parse(date2);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			 Date DATE = new Date();
    		 Date date = new Date(DATE.getTime() + 1000 * 60 * 60 * 24);
    		 String Date = sdf.format(date);
			sb.append(" and a.fcydate <= '"+Date+"' ");
		}
		if(ConstantsUtils.isNotBank(fzzDoc)){
			sb.append(" and a.fzzdoctbh = '"+fzzDoc+"' ");
		}
		if(ConstantsUtils.isNotBank(fprn)){
			sb.append(" and c.fprn = '"+fprn+"' ");
		}
		if(ConstantsUtils.isNotBank(ftimes)){
			sb.append(" and c.ftimes = "+ftimes+" ");
		}
		if(ConstantsUtils.isNotBank(fbarcode)){
			sb.append(" and a.fbarcode = '"+fbarcode+"' ");
		}
		if(ConstantsUtils.isNotBank(fname)){
			sb.append(" and a.fname = '"+fname+"'");
		}
		if(ConstantsUtils.isNotBank(fsex)){
			if(fsex.equals('0')){ //全部
				sb.append(" ");
			}else if(fsex.equals("1")){ //男
				sb.append(" and fsex = '男' ");
			}else if(fsex.equals("2")){
				sb.append(" and fsex = '女'");
			}
		}
		if(ConstantsUtils.isNotBank(fjb)){
			sb.append(" and d.ficdrange = '"+fjb+"'");
		}
		if(ConstantsUtils.isNotBank(category)){
			field = " c.fprn,c.ftimes,a.fsex,a.fzyid ,a.fbarcode,a.fname,a.fcydept,a.frydate,a.fcydate,a.frydept,a.fbirthday,a.fzzdoctbh,d.ficdrange,d.fname as fjbname,b.fcode ";
			table = " SZH_BASE a RIGHT JOIN ( SELECT a.fzyid, b.FCODE FROM SZH_BAFP a LEFT JOIN SZH_BLLXMX b ON a.ftpfl = b.FCODE GROUP BY a.fzyid, b.FCODE ) b ON a.fzyid = b.fzyid "
					+ "left JOIN SZH_BASE c on a.fzyid = c.fzyid  LEFT JOIN tsdisease d  on a.FRYZDBH = d.ficdrange left JOIN SZH_SHOOTRECORD e on b.fzyid = e.fzyid";
			sb.append(" and b.FCODE = '"+category+"'");
		}else{
			field = " c.fprn,c.ftimes,a.fsex,a.fzyid ,a.fbarcode,a.fname,a.fcydept,a.frydate,a.fcydate,a.frydept,a.fbirthday,a.FZZDOCTBH,d.ficdrange,d.fname as fjbname ";
			table = " SZH_BASE a RIGHT JOIN ( SELECT a.fzyid FROM SZH_BAFP a GROUP BY a.fzyid  )b ON a.fzyid = b.fzyid "
					+ "left JOIN SZH_BASE c on a.fzyid = c.fzyid LEFT JOIN tsdisease d ON a.FRYZDBH = d.ficdrange left JOIN SZH_SHOOTRECORD e on b.fzyid = e.fzyid";
		}
		if(ConstantsUtils.isNotBank(fcydept)){
			 String departNumSql = "select fcal from tworkroom where ftykh='" + fcydept + "'";
	            Map<String, Object> departNumMap = this.jdbcTemplate.queryForMap(departNumSql);
	            String departNumStr = departNumMap.get("fcal").toString().replace(",", "','");
	            sb.append(" and a.fcytykh in ('" + departNumStr + "')"); 
		}
		if (ConstantsUtils.isNotBank(page)) {
            currentPage = Integer.valueOf(page);
        }
        if (ConstantsUtils.isNotBank(rows)) {
            pageSize = Integer.valueOf(rows);
        }
		return this.queryMssqlSplitPageByProc(field, table, sb.toString(), "a.fprn", 1, "a.fprn", currentPage, pageSize);
	}

    @Override
    public String watermarkUUID(String code) {
        String sql = "select top 1 UUID from szh_browserecord where fusercode = ?";
        return jdbcTemplate.queryForObject(sql, String.class, code);
    }

	@Override
	public Map<String, Object> transfer(Map<String, Object> map) {
		String name = (String) map.get("name");
		String fcode = (String) map.get("fcode");
		String fzyid = (String) map.get("fzyid");
		String fprn = (String) map.get("fprn");
		int ftimes = (Integer) map.get("ftimes");
		String fname = (String) map.get("fname");
		String fcydate = (String) map.get("fcydate");
		String DATE = (String) map.get("DATE");
		String sql2 = "select fzyid from szh_shootrecord where fzyid = '"+fzyid+"'";
		List<Map<String, Object>> list = this.jdbcTemplate.queryForList(sql2);
		if(list.size() <= 0){
			if(ConstantsUtils.isNotBank(fzyid)){
				//查询当前病案翻拍的张数
				String search = "select count(fzyid) as total from SZH_BAFP where fzyid = '"+ fzyid +"'";
				Map<String, Object> map2 = this.jdbcTemplate.queryForMap(search);
				String sql = "insert into szh_shootrecord ( fzyid, date, fprn, ftimes, fname, fcydate, fstatus, fnumber, fczy, fczybh) "
						+ " values('"+fzyid+"','"+DATE+"','"+fprn+"',"+ftimes+",'"+fname+"','"+fcydate+"','0',"+map2.get("total")+",'"+name+"','"+fcode+"')";
				this.jdbcTemplate.execute(sql);
				map.put("msg", true);
			}else{
				map.put("msg", "error");
			}
		}else{
			map.put("msg", false);
		}
		return map;
	}

	@Override
	public Map<String, Object> auditFail(Map<String, Object> map) {
		String name = (String) map.get("name");
		String savePath = (String) map.get("savePath");
		String originPath = (String) map.get("originPath");
		//String number = (String) map.get("number");
		String categoryNumber = (String) map.get("categoryNumber");
		//String fprn = (String) map.get("fprn");
		//int ftimes = (Integer) map.get("ftimes");
		String barcode = (String) map.get("barcode");
		String fname = (String) map.get("fname");
		String fcode = (String) map.get("fcode");
		String date = (String) map.get("date");
		String fzyid = (String) map.get("fzyid");
	//	String id = (String) map.get("id");
		if(ConstantsUtils.isNotBank(fzyid)){
			String update = "update szh_bafp set fscdz = replace(fscdz,'"+originPath+"','"+savePath+"') where fzyid = '"+ fzyid +"'";
			this.jdbcTemplate.update(update);
			String update2 = "update SZH_SHOOTRECORD set fshr = '"+name+"',fshrbh = '"+fcode+"',fshdate = '"+date+"',fstatus = '2' where fzyid = '"+fzyid+"'";
			this.jdbcTemplate.update(update2);
			map.put("msg", true);
			return map;
		}else{
			map.put("msg", false);
			return map;
		}
	}

	@Override
	public List<Map<String, Object>> getBagdmx(Map<String, Object> params) {
		String sql="select t.*,t.fid id,t.FBLMC+'('+(t.fpages+'页')+')' text,t.FTEMPPATH attributes  from szh_bagdmx t where fgdid in (select max(fid) from dbo.SZH_BAGD where FPRN='"+params.get("fprn")+"' AND FTIMES="+params.get("ftimes")+")";
		return this.jdbcTemplate.queryForList(sql);
	}
}
